'''

Case Type： 审计日志目录
Case Name： 审计过程中审计目录不存在
Create At:  2020/07/01
Owner    :  @wan005
Descption: 1.执行TPCC
2.执行TPCC过程中人为改变审计目录名称
3.检查TPCC结果
4.检查数据库状态
5.恢复审计目录名称
6.执行TPCC
7.执行TPCC过程中删除审计目录
8.检查TPCC结果
9.检查数据库状态
'''
import os
import sys
import unittest
import _thread
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread


class OperAudit(unittest.TestCase):
    log = Logger()
    dbUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRoot = Node(node='PrimaryRoot')
    conf_path = os.path.join(macro.DB_INSTANCE_PATH, macro.DB_PG_CONFIG_NAME)
    pg_audit_path = macro.PG_AUDIT_PATH
    sh = CommonSH('PrimaryDbUser')
    com = Common('PrimaryRoot')
    Constant = Constant()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log.info('------------Opengauss_Reliability_Dbsys_Case038.py start------------')
        self.log.info('----------------------------audit_dml_state =1-----------------------------')
        self.com.replaceFileContentBySed(self.dbUserNode, self.conf_path, "#audit_dml_state = 0", "audit_dml_state =1")
        self.log.info('----------------------------restart opengauss-----------------------------')
        is_stop = self.sh.stopDbCluster()
        self.assertTrue(is_stop)
        is_started = self.sh.startDbCluster()
        self.assertTrue(is_started)

    def test_del_audit(self):
        self.log.info('----------------------------start tpcc-----------------------------')
        tpcc_thread = ComThread(self.com.startTPCC, args=(self.dbPrimaryRoot, macro.TPCC_PATH))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()

        self.log.info('-------------------修改审计日志名称-----------------------')
        cmd = f'mv {self.pg_audit_path}  {self.pg_audit_path}.baktest'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.assertIn('', msg)
        self.log.info(msg)
        msg = self.dbUserNode.sh(f'ls -al {self.pg_audit_path}').result()
        self.log.info(msg)
        msg = self.dbUserNode.sh(f'ls -al {self.pg_audit_path}.baktest').result()
        self.log.info(msg)

        self.log.info('--------------check tpcc result--------------------------- ')
        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info('--------------check opengauss status--------------------------- ')
        db_status = self.sh.getDbClusterStatus('status')
        self.assertTrue(db_status)
        
        self.log.info('-------------------恢复审计日志名称-----------------------')
        cmd = f'mv  {self.pg_audit_path}.baktest {self.pg_audit_path}'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.log.info(msg)
        self.assertIn('', msg)
        msg = self.dbUserNode.sh(f'ls -al {self.pg_audit_path}').result()
        self.log.info(msg)
        msg = self.dbUserNode.sh(f'ls -al {self.pg_audit_path}.baktest').result()
        self.log.info(msg)

        self.log.info('----------------------------start tpcc-----------------------------')
        tpcc_thread = ComThread(self.com.startTPCC, args=(self.dbPrimaryRoot, macro.TPCC_PATH))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()

        self.log.info('-------------------删除审计日志目录--------------')
        cmd = f'mv {self.pg_audit_path}  {macro.DB_BACKUP_PATH}'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.log.info(msg)
        self.assertIn('', msg)

        time.sleep(3)

        self.log.info('--------------check tpcc result--------------------------- ')
        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info('--------------check opengauss status--------------------------- ')
        db_status = self.sh.getDbClusterStatus('status')
        self.assertTrue(db_status)

    def tearDown(self):
        self.log.info('--------------this is tearDown--------------------------- ')
        self.log.info('-------------------恢复审计日志名称-----------------------')
        cmd = f'mv  {self.pg_audit_path}.baktest {self.pg_audit_path}'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.log.info(msg)

        cmd = f'mv {os.path.join(macro.DB_BACKUP_PATH, os.path.basename(self.pg_audit_path))} {os.path.dirname(self.pg_audit_path)}'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.log.info(msg)
